
import { _decorator, Component, Node ,TiledMap ,LabelComponent} from 'cc';
import {ClientEvent} from "../../framework/clientEvent";
import {Constant} from "../../framework/constant";
import {LogicManager} from "../../core/logicManager";
import {Player} from "../../core/GameState";
import i18n from "../../i18nMaster/runtime-scripts/LanguageData";
const { ccclass, property } = _decorator;

/**
 * Predefined variables
 * Name = FightUI
 * DateTime = Mon Dec 20 2021 16:36:06 GMT+0800 (中国标准时间)
 * Author = yly1234560
 * FileBasename = fightUI.ts
 * FileBasenameNoExtension = fightUI
 * URL = db://assets/scripts/ui/fight/fightUI.ts
 * ManualUrl = https://docs.cocos.com/creator/3.3/manual/zh/
 *
 */
 
@ccclass('FightUI')
export class FightUI extends Component {
    @property(LogicManager)
    public logicManager: LogicManager = null!;

    @property([LabelComponent])
    public aryPlayerName: LabelComponent[] = [];
    @property([LabelComponent])
    public aryTime: LabelComponent[] = [];

    // 计时器回调
    private _scheduleCallback:{};

    onEnable() {
        ClientEvent.on(Constant.FIGHT_UI.INIT_FIGHT_PLAYER, this._initPlayer, this);
        ClientEvent.on(Constant.FIGHT_UI.ON_COUNT_DOWN_START, this._startTimeCutDown, this);
        ClientEvent.on(Constant.FIGHT_UI.ON_COUNT_DOWN_STOP, this._stopTimeCutDown, this);
    }

    onDisable() {
        ClientEvent.off(Constant.FIGHT_UI.INIT_FIGHT_PLAYER, this._initPlayer, this);
        ClientEvent.off(Constant.FIGHT_UI.ON_COUNT_DOWN_START, this._startTimeCutDown, this);
        ClientEvent.off(Constant.FIGHT_UI.ON_COUNT_DOWN_STOP, this._stopTimeCutDown, this);
    }

    // 初始化人物信息
    private _initPlayer(){
        let players: Array<Player> = this.logicManager.currentGameState.players;
        for (let idx = 0; idx < players.length; idx++) {
            let player: Player = players[idx];
            if (!player.channel) {
                continue;
            }

            if (player.channel.openId) {
                this.aryPlayerName[idx].string = player.channel.name;
            }
        }

        // 开始倒计时
        this._startTimeCutDown(0,30);
    }


    start(){
        // this._startTimeCutDown(0,30);
    }

    // 开始倒计时
    private _startTimeCutDown(camp:number,time){
        this.unschedule(this._scheduleCallback);
        // time = 5;
        let allTime = time;
        let startOsTime = Date.now();
        this.aryTime[camp].string = allTime;
        this._scheduleCallback = function(){
            let nowOsTime = Date.now();
            let nowTime = allTime - Math.floor((nowOsTime - startOsTime)/1000);
            if(nowTime <= 0){
                this._stopTimeCutDown(camp);
                ClientEvent.dispatchEvent(Constant.EVENT_NAME.ON_ROUND_TIME_OUT,camp);
                return;
            }
            this.aryTime[camp].string = nowTime;
        };
        this.schedule(this._scheduleCallback, 1);
    }
    // 停止倒计时
    private _stopTimeCutDown(){
        this.unschedule(this._scheduleCallback);
        this.aryTime[0].string = "--";
        this.aryTime[1].string = "--";
    }


}

/**
 * [1] Class member could be defined like this.
 * [2] Use `property` decorator if your want the member to be serializable.
 * [3] Your initialization goes here.
 * [4] Your update function goes here.
 *
 * Learn more about scripting: https://docs.cocos.com/creator/3.3/manual/zh/scripting/
 * Learn more about CCClass: https://docs.cocos.com/creator/3.3/manual/zh/scripting/ccclass.html
 * Learn more about life-cycle callbacks: https://docs.cocos.com/creator/3.3/manual/zh/scripting/life-cycle-callbacks.html
 */
